/**
 * 
 */
package com.hfjyz.registration.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.registration.command.AnnouncementListCommand;
import com.hfjyz.registration.command.DepartListCommand;
import com.hfjyz.registration.command.OfficeEditCommand;
import com.hfjyz.registration.command.OfficeListCommand;
import com.hfjyz.registration.dao.DepartDao;
import com.hfjyz.registration.dao.ExamDao;
import com.hfjyz.registration.dao.OfficeDao;
import com.hfjyz.registration.model.Depart;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.model.Office;
import com.hfjyz.registration.util.MasterUtil;

/**
 *
 */
public class OfficeServiceImpl extends AbstractServiceImpl implements
		OfficeService {
	private ExamDao examDao;
	private DepartDao departDao;
	private OfficeDao officeDao;

	@SuppressWarnings("rawtypes")
	@Override
	public Map initEdit(OfficeEditCommand cmd, Errors errors) throws Exception {
		if (cmd != null && cmd.getId() != null) {
			Office office = officeDao.get(cmd.getId());
			if (office != null) {
				cmd.setId(office.getId());
				cmd.setName(office.getName());
				cmd.setDescription(office.getDescription());
				cmd.setExamId(office.getExamId());
				cmd.setCode(office.getCode());
				cmd.setDegree(office.getDegree());
				cmd.setMajor(office.getMajor());
				cmd.setRecruits(String.valueOf(office.getRecruits()));
				cmd.setDepartId(office.getDepartId());
			}
		}
		AnnouncementListCommand alc = new AnnouncementListCommand();
		alc.setStatus(MasterUtil.getInstance().getMaster(10, "07").getId());
		List<Exam> exams = examDao.search(alc);
		cmd.setExams(exams);

		DepartListCommand dlc = new DepartListCommand();
		dlc.setExamId(cmd.getExamId());
		cmd.setDeparts(departDao.search(dlc));
		return null;
	}

	@Transactional
	@Override
	public void editOk(HttpServletRequest request, OfficeEditCommand cmd,
			BindException errors) throws Exception {

		Office office = new Office();
		office.setId(cmd.getId());
		office.setName(cmd.getName());
		office.setDescription(cmd.getDescription());
		office.setExamId(cmd.getExamId());
		office.setCode(cmd.getCode());
		office.setDegree(cmd.getDegree());
		office.setMajor(cmd.getMajor());
		office.setRecruits(Integer.parseInt(cmd.getRecruits()));
		if (cmd.getDepartId() != null && cmd.getDepartId() != -1) {
			office.setDepartId(cmd.getDepartId());
		}
		if (office.getId() == null) {
			officeDao.add(office);
		} else {
			officeDao.update(office);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Map initSearch(OfficeListCommand cmd, Errors errors)
			throws Exception {
		AnnouncementListCommand alc = new AnnouncementListCommand();
		alc.setStatus(MasterUtil.getInstance().getMaster(10, "07").getId());
		// alc.setEid(cmd.getExamId());
		List<Exam> exams = examDao.search(alc);
		cmd.setExams(exams);

		DepartListCommand dlc = new DepartListCommand();
		// dlc.setExamId(cmd.getExamId());
		// dlc.setDid(cmd.getDepartId());
		List<Depart> departs = departDao.search(dlc);
		Depart d = new Depart();
		d.setId(-1);
		d.setName("");
		departs.add(0, d);
		cmd.setDeparts(departs);
		
		if (cmd.getExamId() == null && exams != null && !exams.isEmpty()) {
			cmd.setExamId(exams.get(0).getId());
		}
		List<Office> offices = officeDao.search(cmd);
		cmd.setResult(offices);
		return null;
	}

	@Override
	public void search(HttpServletRequest request, OfficeListCommand cmd)
			throws Exception {
		List<Office> offices = officeDao.search(cmd);
		cmd.setResult(offices);
	}

	@Transactional
	@Override
	public void delete(HttpServletRequest request, OfficeListCommand cmd,
			BindException errors) throws Exception {
		if (cmd.getOid() != null) {
			officeDao.delete(officeDao.get(cmd.getOid()));
		}
	}

	/**
	 * @return the officeDao
	 */
	public OfficeDao getOfficeDao() {
		return officeDao;
	}

	/**
	 * @param officeDao
	 *            the officeDao to set
	 */
	public void setOfficeDao(OfficeDao officeDao) {
		this.officeDao = officeDao;
	}

	/**
	 * @return the examDao
	 */
	public ExamDao getExamDao() {
		return examDao;
	}

	/**
	 * @param examDao
	 *            the examDao to set
	 */
	public void setExamDao(ExamDao examDao) {
		this.examDao = examDao;
	}

	/**
	 * @return the departDao
	 */
	public DepartDao getDepartDao() {
		return departDao;
	}

	/**
	 * @param departDao
	 *            the departDao to set
	 */
	public void setDepartDao(DepartDao departDao) {
		this.departDao = departDao;
	}

}
